<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工编号" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入员工编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wages:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wages:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wagesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工资编号" align="center" prop="wid" />
      <el-table-column label="员工编号" align="center" prop="userid" />
      <el-table-column label="员工姓名" align="center" prop="username" />
      <el-table-column label="员工部门" align="center" prop="department" />
      <el-table-column label="岗位" align="center" prop="deptdetailid" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="账单类型" align="center" prop="bill" />


      <el-table-column label="工资明细" align="center" prop="wages">

        <template slot-scope="scope">
          <el-button @click="drawer = true" type="primary" style="margin-left: 21px;">
            详情
          </el-button>

          <el-drawer
            title="我是标题"
            :visible.sync="drawer"
            :with-header="false">
            <span>
              <el-table v-loading="loading" :data="wagesList" @selection-change="handleSelectionChange">

                <el-table-column label="员工编号" align="center" prop="userid" />
                              <el-table-column label="基本工资" align="center" prop="essential" />
                              <el-table-column label="加班费" align="center" prop="overtime" />
                              <el-table-column label="奖金" align="center" prop="bonus" />
                              <el-table-column label="罚款" align="center" prop="penalty" />
                              <el-table-column label="实际工资" align="center" prop="actual" />

              </el-table>
            </span>
          </el-drawer>
        </template>

      </el-table-column>



      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:wages:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wages:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工资信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工编号" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入员工编号" />
        </el-form-item>
        <el-form-item label="员工姓名" prop="username">

          <el-select v-model="form.username" filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.username"
              :label="item.storeName"
              :value="item.username">
            </el-option>
          </el-select>

<!--          <el-input v-model="form.username" placeholder="请输入员工姓名" />-->
        </el-form-item>
        <el-form-item label="员工部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入员工部门" />
        </el-form-item>
        <el-form-item label="岗位" prop="deptdetailid">
          <el-input v-model="form.deptdetailid" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="账单类型" prop="bill">
          <el-input v-model="form.bill" placeholder="请输入账单类型" />
        </el-form-item>

<!--        <el-form-item label="工资明细" prop="wages">-->
<!--          <el-input v-model="form.wages" placeholder="请输入工资明细" />-->
<!--        </el-form-item>-->

        <el-form-item label="基本工资" prop="essential">
          <el-input v-model="form.essential" placeholder="请输入基本工资" />
        </el-form-item>
        <el-form-item label="加班费" prop="overtime">
          <el-input v-model="form.overtime" placeholder="请输入加班费" />
        </el-form-item>
        <el-form-item label="奖金" prop="bonus">
          <el-input v-model="form.bonus" placeholder="请输入奖金" />
        </el-form-item>
        <el-form-item label="罚款" prop="penalty">
          <el-input v-model="form.penalty" placeholder="请输入罚款" />
        </el-form-item>
        <el-form-item label="实际工资" prop="actual">
          <el-input v-model="form.actual" placeholder="请输入实际工资" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listgzxx, listWages, getWages, delWages, addWages, updateWages } from "@/api/system/wages";

export default {
  name: "Wages",
  data() {
    return {

      drawer: false,//抽屉

      options: [], //存储所以的mingz信息

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工资信息管理表格数据
      wagesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userid: null,
        username: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userid: [
          { required: true, message: "员工编号不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        essential: [
          { required: true, message: "基本工资不能为空", trigger: "blur" }
        ],
        actual: [
          { required: true, message: "实际工资不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工资信息管理列表 */
    getList() {
      this.loading = true;
      listWages(this.queryParams).then(response => {
        this.wagesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wid: null,
        userid: null,
        username: null,
        department: null,
        deptdetailid: null,
        amount: null,
        bill: null,
        wages: null,
        essential: null,
        overtime: null,
        bonus: null,
        penalty: null,
        actual: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工资信息管理";

      //调用后端展示所有名字，下拉框展示

      listgzxx().then(aaa =>{
        console.log("结果： ",JSON.stringify(aaa))
        this.options = aaa.data
      })

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wid = row.wid || this.ids
      getWages(wid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工资信息管理";
      });

      //调用后端展示所有名字，下拉框展示

      listgzxx().then(aaa =>{
        console.log("结果： ",JSON.stringify(aaa))
        this.options = aaa.data
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wid != null) {
            updateWages(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWages(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wids = row.wid || this.ids;
      this.$modal.confirm('是否确认删除工资信息管理编号为"' + wids + '"的数据项？').then(function() {
        return delWages(wids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wages/export', {
        ...this.queryParams
      }, `wages_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
